//
//  SEGRedDotManager.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2018/3/7.
//  Copyright © 2018年 陈区. All rights reserved.
//

#import "SEGRedDotManager.h"
#if __has_include(<Commom/SEGCommonConfig.h>)
#import <Commom/SEGCommonConfig.h>
#else
#import "SEGCommonConfig.h"
#endif

@implementation SEGRedDotManager
/**
 * 保存菜单父子关系
 * @param array  菜单模型数组
 * @param parentsSid  当前菜单的父settingsId
 * @return Type和Rescode对应关系字典
 */
+ (void)saveMenuListWithArray:(NSArray <SEGMenuListModel *>*)array parentsSid:(NSString *)parentsSid
{
    //取出当前用户和小区的数据
    NSMutableDictionary *dict = [self seg_getRescodeAndSettingsIdsDict];

    
    if ([parentsSid isEqualToString:@"0"]) {//tab菜单，直接保存有rescode的数据
        [array enumerateObjectsUsingBlock:^(SEGMenuListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.resCode.length > 0 && obj.settingsId.length > 0) {
                [dict setObject:@[obj.settingsId] forKey:obj.resCode];
            }
        }];
    }else {
        
        //先删除相关老数据，防止多余数据
        [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSArray *settingsIdArr, BOOL * _Nonnull stop) {
            NSMutableArray *tempArr = [NSMutableArray arrayWithArray:settingsIdArr];
            [settingsIdArr enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL * _Nonnull stop) {
                //删除所有包含parentsSid的settingsId串（即这个菜单的所有子菜单的红点相关对应关系数据）
                if ([obj containsString:parentsSid] && (![obj isEqualToString:parentsSid])) {
                    [tempArr removeObject:obj];
                }
            }];
            if(tempArr.count > 0) {
                [dict setObject:tempArr forKey:key];
            }else {
                [dict removeObjectForKey:key];
            }
        }];
        
        
        //菜单拼接
        [self seg_saveChildListWithArray:array dict:dict parentsSid:parentsSid];
    }
    
    //保存数据
    [self seg_saveDataToUserDefaultWithKey:kRescodeAndSettingIdsKey data:dict];
    
    //保存后判断是否有本地保存的红点信息
    NSArray *rescodesArray = [self seg_getNotFoundRescodeArray];
    if (rescodesArray.count > 0) {
        //从菜单父子关系表中找出对应的ids
        NSDictionary *currentDict = [self seg_getRescodeAndSettingsIdsDict];
        [rescodesArray enumerateObjectsUsingBlock:^(id  _Nonnull rescode, NSUInteger idx, BOOL * _Nonnull stop) {
            NSArray *idsArray = [currentDict objectForKey:rescode];
            if (idsArray && idsArray.count > 0) {
                [idsArray enumerateObjectsUsingBlock:^(NSString *ids, NSUInteger idx, BOOL * _Nonnull stop) {
                    //保存新的id到本地
                    NSArray *newIds = [ids componentsSeparatedByString:@","];
                    [self seg_saveNewSettingsIds:newIds];
                    //删除对应的本地rescode
                    [self seg_deleteNotFoundRescode:rescode];
                    
                    //只刷新当前的推送涉及的id
                    [[NSNotificationCenter defaultCenter] postNotificationName:SEGAddReddotNotification object:newIds];
                }];
            }
        }];
    }
    // 处理未点击的红点逻辑
    NSMutableArray *cacheIdsArray = [self getCurrentSettingIdsArray];
    if (cacheIdsArray.count > 0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:SEGAddReddotNotification object:cacheIdsArray];
    }
}

#pragma mark - 红点ID相关方法
// 获取现有的红点settingsId数组
+ (NSMutableArray *)getCurrentSettingIdsArray
{
    NSMutableDictionary *allIdsDict = [self seg_getAllUserAndCommunityDictWithKey:kRedDotSettingIdsKey];
    NSMutableArray *currentIdsArray = [NSMutableArray array];
    NSArray *tempIDsArray = [allIdsDict objectForKey:[self seg_currentUserAndCommunity]];
    if (tempIDsArray && tempIDsArray.count > 0) {
        [currentIdsArray addObjectsFromArray:tempIDsArray];
    }
    return currentIdsArray;
}

/**
 * 获取红点的Type和Rescode对应关系字典
 * @return Type和Rescode对应关系字典
 */
+ (NSDictionary *)getRedDotTypeAndRescodeDict
{
    NSDictionary *dict = @{
                           @"C_PUSH_YOUZAN" : @"RES_MESSAGE",//有赞订单
                           @"C_PUSH_NOTICE_NEIGHBORHOODMORE" : @"RES_MESSAGE",//头条被推荐通知
                           @"C_PUSH_TOPIC" : @"RES_MESSAGE",//话题
                           @"C_PUSH_INTEGRAL" : @"RES_MESSAGE",//积分商城
                           @"C_PUSH_PGC" : @"RES_MESSAGE",//C_PUSH_PGC
                           @"C_PUSH_NOTICE_INTEGRAL" : @"RES_MESSAGE",//积分商城
                           @"100200" : @"RES_MESSAGE",
                           @"100310" : @"RES_MESSAGE",
                           @"100320" : @"RES_MESSAGE",
                           @"100330" : @"RES_MESSAGE",
                           @"100331" : @"RES_MESSAGE",
                           @"100340" : @"RES_MESSAGE",
                           @"100341" : @"RES_MESSAGE",
                           @"100400" : @"RES_MESSAGE",
                           @"100410" : @"RES_MESSAGE",
                           @"101300" : @"RES_MESSAGE",
                           @"101600" : @"RES_MESSAGE",
                           @"101800" : @"RES_MESSAGE",
                           @"101900" : @"RES_MESSAGE",
                           @"102000" : @"RES_MESSAGE",
                           @"102200" : @"RES_MESSAGE",
                           @"200000" : @"RES_MESSAGE",
                           @"300004" : @"RES_MESSAGE",
                           @"500000" : @"RES_MESSAGE",
                           @"50002" : @"RES_MESSAGE",
                           @"50003" : @"RES_MESSAGE",
                           @"50004" : @"RES_MESSAGE",
                           @"50005" : @"RES_MESSAGE",
                           @"accessToPersonal" : @"RES_MESSAGE",
                           @"addToFavorites" : @"RES_MESSAGE",
                           @"auditFail" : @"RES_MESSAGE",
                           @"auth" : @"RES_MESSAGE",
                           @"beLike" : @"RES_MESSAGE",
                           @"cancelCollect" : @"RES_MESSAGE",
                           @"cancelLike" : @"RES_MESSAGE",
                           @"commendSeller" : @"RES_MESSAGE",
                           @"commentService" : @"RES_MESSAGE",
                           @"completeInformation" : @"RES_MESSAGE",
                           @"delShareGift" : @"RES_MESSAGE",
                           @"downgrade" : @"RES_MESSAGE",
                           @"exChangeRecord" : @"RES_MESSAGE",
                           @"fornumIsDeleted" : @"RES_MESSAGE",
                           @"idle" : @"RES_MESSAGE",
                           @"invitationSuccess" : @"RES_MESSAGE",
                           @"like" : @"RES_MESSAGE",
                           @"login" : @"RES_MESSAGE",
                           @"openDoor" : @"RES_MESSAGE",
                           @"operatorOrdinary" : @"RES_MESSAGE",
                           @"payManagementFee" : @"RES_MESSAGE",
                           @"personalInfarmation" : @"RES_MESSAGE",
                           @"pgc" : @"RES_MESSAGE",
                           @"postForCarpooling" : @"RES_MESSAGE",
                           @"postForHelp" : @"RES_MESSAGE",
                           @"purchanceProduct" : @"RES_MESSAGE",
                           @"push_sharing_courtesy" : @"RES_INVITING_NEIGHBORS",
                           @"questionNaire" : @"RES_MESSAGE",
                           @"quiltInvitationSuccess" : @"RES_MESSAGE",
                           @"repair" : @"RES_MESSAGE",
                           @"reply" : @"RES_MESSAGE",
                           @"replyIsDeleted" : @"RES_MESSAGE",
                           @"saleService" : @"RES_MESSAGE",
                           @"share" : @"RES_MESSAGE",
                           @"shareGift" : @"RES_MESSAGE",
                           @"signUp" : @"RES_MESSAGE",
                           @"topic" : @"RES_MESSAGE",
                           @"updateProduct" : @"RES_MESSAGE",
                           @"upgrade" : @"RES_MESSAGE",
                           @"visitor" : @"RES_MESSAGE",
                           @"C_PUSH_NOTICE_PRIZE_ACTIVITY" : @"RES_MESSAGE",
                           @"C_PUSH_NOTICE_PRIZE_TOPIC" : @"RES_MESSAGE",
                           @"C_PUSH_SILENT_ON" : @"RES_MESSAGE",
                           @"C_PUSH_SILENT_OFF" : @"RES_MESSAGE",
                           @"C_PUSH_PGC_DELETE" : @"RES_MESSAGE",
                           kMessageTypeNotice : @"RES_MESSAGE", // 外部链接
                           @"C_PUSH_INVITE_NEIGHBOR" : @"RES_MESSAGE", //邀请邻居
                           @"C_PUSH_CUSTOM" : @"RES_MESSAGE", //自定义消息
                           @"100777" : @"RES_PROPERTY_SERVICE",
                           @"100888" : @"RES_HOME_SERVICE,RES_MESSAGE",//到家服务
                           @"101500" : @"RES_GROUP_ORDER",
                           @"C_NEIGHBORHOOD_NEWS" : @"RES_NEIGHBOR",// 邻里圈新动态
                           kMessageTypeActivityTask: @"RES_MESSAGE", // 任务 - 中奖记录
                           @"SANY_USER_AUDIT_PASS" : @"RES_MESSAGE", // sanyi用户审核通过
                           @"SANY_USER_AUTI_UNPASS" : @"RES_MESSAGE", // sanyi用户审核失败
                           @"SANY_ACCOUNT_UPGRDE" : @"RES_MESSAGE", // sanyi用户升级管理员
                           };
    
    NSMutableDictionary *totalDict = [NSMutableDictionary dictionaryWithDictionary:dict];
    NSMutableString *resCode = [NSMutableString stringWithString:@"RES_PROPERTY_PUBLIC_SERVICE"]; // 默认为显示在物业通知所在的菜单
    if (![SEGCommonConfig shared].isHideServiceMessage) { // 有服务号
        [resCode appendString:@",RES_MESSAGE"];
    }
    NSArray *serviceTypeArray = [self getServiceTypeArray];
    [serviceTypeArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [totalDict setValue:resCode forKey:obj];
    }];
    //物业服务消息还需要对应物业服务菜单控件
    [totalDict setValue:[NSString stringWithFormat:@"%@,RES_PROPERTY_SERVICE",resCode] forKey:@"100700"];
    return totalDict;
}

/**
 * 保存红点的Type对应的菜单ID
 *  @param type 推送或透传消息的type
 */
+ (void)saveRedDotWithType:(NSString *)type
{
    NSDictionary* dict = [self getRedDotTypeAndRescodeDict];
    NSString *rescodes = [dict objectForKey:type];
    if (rescodes && rescodes.length > 0) {
        NSArray *rescodesArray = [rescodes componentsSeparatedByString:@","];
        if (rescodesArray.count > 0) {//有对应的rescodes才需要打红点
            //从菜单父子关系表中找出对应的ids
            NSDictionary *currentDict = [self seg_getRescodeAndSettingsIdsDict];
            [rescodesArray enumerateObjectsUsingBlock:^(id  _Nonnull rescode, NSUInteger idx, BOOL * _Nonnull stop) {
                NSArray *idsArray = [currentDict objectForKey:rescode];
                if (idsArray && idsArray.count > 0) {
                    [idsArray enumerateObjectsUsingBlock:^(NSString *ids, NSUInteger idx, BOOL * _Nonnull stop) {
                        //保存新的id到本地
                        NSArray *newIds = [ids componentsSeparatedByString:@","];
                        [self seg_saveNewSettingsIds:newIds];

                        //只刷新当前的推送涉及的id
                        [[NSNotificationCenter defaultCenter] postNotificationName:SEGAddReddotNotification object:newIds];
                    }];
                }else {
                    //还没有对应的id,说明菜单还没有请求到，先缓存rescode
                    [self seg_saveNotFoundRescode:rescode];
                }
            }];
        }
    }
}

/**
 *  删除Type对应的本地红点信息
 *  @param type 推送或透传消息的type
 */
+ (void)deleteRedDotWithType:(NSString *)type
{
    NSDictionary* dict = [self getRedDotTypeAndRescodeDict];
    NSString *rescodes = [dict objectForKey:type];
    if (rescodes && rescodes.length > 0) { //有对应的rescodes才需要打红点
        //从菜单父子关系表中找出对应的ids
        NSDictionary *currentDict = [self seg_getRescodeAndSettingsIdsDict];
        NSArray *rescodesArray = [rescodes componentsSeparatedByString:@","];
        [rescodesArray enumerateObjectsUsingBlock:^(id  _Nonnull rescode, NSUInteger idx, BOOL * _Nonnull stop) {
            NSArray *idsArray = [currentDict objectForKey:rescode];
            [idsArray enumerateObjectsUsingBlock:^(NSString *ids, NSUInteger idx, BOOL * _Nonnull stop) {
                //删除对应的id
                NSArray *newIdsArray = [ids componentsSeparatedByString:@","];
                NSMutableArray *currentIdsArray = [self getCurrentSettingIdsArray];
                [newIdsArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([currentIdsArray containsObject:obj]) {
                        [currentIdsArray removeObject:obj];
                    }
                }];
                //保存红点数据
                [self seg_saveDataToUserDefaultWithKey:kRedDotSettingIdsKey data:currentIdsArray];
                //只刷新当前的推送涉及的id
                //发送的是不显示红点的数组
                [[NSNotificationCenter defaultCenter] postNotificationName:SEGHiddenRedDotNotification object:newIdsArray];
                
            }];
        }];
    }
}

/**
 * 删除菜单ID对应的本地红点信息
 * @param idString 菜单的settingsId
 */
+ (void)deleteRedDotWithId:(NSString *)idString
{
    //取出现有的ID
    NSMutableArray *currentIdsArray = [self getCurrentSettingIdsArray];
    if ([currentIdsArray containsObject:idString]) {
        [currentIdsArray removeObject:idString];
        //取出父子菜单数据
        NSMutableDictionary *dict = [self seg_getRescodeAndSettingsIdsDict];
        //获取所有的父菜单id
        NSMutableArray *parentIdsArr = [NSMutableArray array];
        NSArray *allIdsArray = [dict allValues];
        [allIdsArray enumerateObjectsUsingBlock:^(NSArray *idsArray, NSUInteger idx, BOOL * _Nonnull stop) {
            [idsArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSRange range = [obj rangeOfString:idString];
                if (range.location != NSNotFound) {
                    NSString *ids = [obj substringWithRange:range];
                    NSArray *idsArr = [ids componentsSeparatedByString:@","];
                    [idsArr enumerateObjectsUsingBlock:^(id  _Nonnull obj1, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (![parentIdsArr containsObject:obj1]) {
                            [parentIdsArr addObject:obj1];
                        }
                    }];
                }
            }];
        }];
        
        //删除所有父菜单红点
        [parentIdsArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([currentIdsArray containsObject:obj]) {
                [currentIdsArray removeObject:obj];
            }
        }];
        
        //保存红点数据
        [self seg_saveDataToUserDefaultWithKey:kRedDotSettingIdsKey data:currentIdsArray];
        
        //发送刷新通知 消失父菜单红点
        //发送的是不显示红点的数组
        [[NSNotificationCenter defaultCenter] postNotificationName:SEGHiddenRedDotNotification object:parentIdsArr];
    }
}

#pragma mark - 物业服务消息相关方法
/**
 * 获取所有物业服务的消息类型数组
 * @return 物业服务的消息类型数组
 */
+ (NSArray *)getServiceTypeArray
{
    /**
     物业服务的消息 跳转到物业服务列表
     */
    NSArray *serviceTypeArray = @[@"101000",//物业服务的消息 跳转到物业服务列表
                                  @"100100",//账单：101000
                                  @"100500",@"100600"//快递：100500、100600
                                  ,@"100700",//工单：100700
                                  @"1",@"C_PUSH_AUTH_NO",//用户认证通过：1 用户认证不通过：C_PUSH_AUTH_NO
                                  @"C_PUSH_TYPE_WJ",//问卷：C_PUSH_TYPE_WJ
                                  @"300000",//用户审核：300000
                                  @"B_PUSH_TYPE_CHARING_YKSH",//月卡审核 成功失败
                                  @"B_PUSH_TYPE_CARD_ACTIVE_REMIND",//月卡激活
                                  @"B_PUSH_TYPE_CARD_EXPIRE_REMIND",//月卡失效
                                  @"B_PUSH_TYPE_CHARING_ADD_CAR",//新增车辆 成功失败
                                  kMessageTypeHouseRentalAndSale,//租售消息
                                  kMessageTypeCarRentalAndSale,//车位租售
                                  kMessageTypeOwnerAuth, // 住户审核
                                  @"C_PUSH_LEASE_EXPIRES", // 租赁到期
                                  @"SANY_USER_AUDIT_PASS", // sanyi用户审核通过
                                  @"SANY_USER_AUTI_UNPASS", // sanyi用户审核失败
                                  @"SANY_ACCOUNT_UPGRDE", //sanyi用户升级管理员
                                  @"C_PUSH_TYPE_AUDIT_COMMENT", //审核评论
                                  @"C_PUSH_TYPE_AUDIT_POST"//审核帖子
                                  ];
    return serviceTypeArray;
}

/**
 *  获取现有的物业服务消息serviceId数组
 */
+ (NSMutableArray *)getMyServiceIdArray
{
    NSMutableDictionary *allIdsDict = [self seg_getAllUserAndCommunityDictWithKey:KMyServiceIdsKey];
    NSMutableArray *currentIdsArray = [NSMutableArray array];
    NSArray *tempIDsArray = [allIdsDict objectForKey:[self seg_currentUserAndCommunity]];
    if (tempIDsArray && tempIDsArray.count > 0) {
        [currentIdsArray addObjectsFromArray:tempIDsArray];
    }
    return currentIdsArray;
}

/**
 *  保存物业服务消息的serviceId
 *  @param serviceId 推送或透传消息的serviceId
 */
+ (void)saveMyServiceId:(NSString *)serviceId
{
    NSMutableArray *currentIdsArray = [self getMyServiceIdArray];
    if (![currentIdsArray containsObject:serviceId]) {
        [currentIdsArray addObject:serviceId];
    }
    //保存红点数据
    [self seg_saveDataToUserDefaultWithKey:KMyServiceIdsKey data:currentIdsArray];
}

/**
 *  删除物业服务消息的serviceId
 *  @param serviceId 推送或透传消息的serviceId
 */
+ (void)deleteMyServiceId:(NSString *)serviceId
{
    NSMutableArray *currentIdsArray = [self getMyServiceIdArray];
    if ([currentIdsArray containsObject:serviceId]) {
        [currentIdsArray removeObject:serviceId];
    }
    //保存红点数据
    [self seg_saveDataToUserDefaultWithKey:KMyServiceIdsKey data:currentIdsArray];
}

#pragma mark - 私有方法
/**
 *  获取现有的NotFoundRescode数组
 */
+ (NSMutableArray *)seg_getNotFoundRescodeArray
{
    NSMutableDictionary *allIdsDict = [self seg_getAllUserAndCommunityDictWithKey:KNotFoundRescodesKey];
    NSMutableArray *currentIdsArray = [NSMutableArray array];
    NSArray *tempIDsArray = [allIdsDict objectForKey:[self seg_currentUserAndCommunity]];
    if (tempIDsArray && tempIDsArray.count > 0) {
        [currentIdsArray addObjectsFromArray:tempIDsArray];
    }
    return currentIdsArray;
}

/**
 *  保存物业服务消息的NotFoundRescode
 *  @param rescode 推送或透传消息的rescode
 */
+ (void)seg_saveNotFoundRescode:(NSString *)rescode
{
    NSMutableArray *currentIdsArray = [self seg_getNotFoundRescodeArray];
    if (![currentIdsArray containsObject:rescode]) {
        [currentIdsArray addObject:rescode];
    }
    //保存红点数据
    [self seg_saveDataToUserDefaultWithKey:KNotFoundRescodesKey data:currentIdsArray];
}

/**
 *  删除物NotFoundRescode
 *  @param rescode 推送或透传消息的serviceId
 */
+ (void)seg_deleteNotFoundRescode:(NSString *)rescode
{
    NSMutableArray *currentIdsArray = [self seg_getNotFoundRescodeArray];
    if ([currentIdsArray containsObject:rescode]) {
        [currentIdsArray removeObject:rescode];
    }
    //保存红点数据
    [self seg_saveDataToUserDefaultWithKey:KNotFoundRescodesKey data:currentIdsArray];
}

+ (NSMutableDictionary *)seg_getRescodeAndSettingsIdsDict
{
    //取出所有用户和小区的数据
    NSMutableDictionary *allDict = [self seg_getAllUserAndCommunityDictWithKey:kRescodeAndSettingIdsKey];
    //取出当前用户和小区的数据
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if ([allDict objectForKey:[self seg_currentUserAndCommunity]]) {
        NSDictionary *currentDict = [allDict objectForKey:[self seg_currentUserAndCommunity]];
        [dict addEntriesFromDictionary:currentDict];
    }
    return dict;
}

// 递归遍历菜单
+ (void)seg_saveChildListWithArray:(NSArray <SEGMenuListModel *>*)array dict:(NSMutableDictionary *)dict parentsSid:(NSString *)parentsSid
{
    [array enumerateObjectsUsingBlock:^(SEGMenuListModel * _Nonnull obj1, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *ids = [NSString stringWithFormat:@"%@,%@",parentsSid, obj1.settingsId];
        if (obj1.resCode.length > 0 && obj1.settingsId.length > 0) {
            //防止一个菜单多个入口,存成ids数组
            NSMutableArray *idsArray = [NSMutableArray array];
            NSArray *tempIds = [dict objectForKey:obj1.resCode];
            if(tempIds && tempIds.count > 0) {
                [idsArray addObjectsFromArray:tempIds];
            }
            if (![idsArray containsObject:ids]) {
                [idsArray addObject:ids];
            }
            [dict setObject:idsArray forKey:obj1.resCode];
        }
        if (obj1.childList && [obj1.childList isKindOfClass:[NSArray class]] && obj1.childList.count > 0) {
            [self seg_saveChildListWithArray:obj1.childList dict:dict parentsSid:ids];
        }
    }];
}

// 保存settingsId数组
+ (void)seg_saveNewSettingsIds:(NSArray *)newIdsArray
{
    //取出现有的ID
    NSMutableArray *currentIdsArray = [self getCurrentSettingIdsArray];
    //把新的ID添加进去
    [newIdsArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (![currentIdsArray containsObject:obj]) {
            [currentIdsArray addObject:obj];
        }
    }];
    //保存红点数据
    [self seg_saveDataToUserDefaultWithKey:kRedDotSettingIdsKey data:currentIdsArray];
}

//获取key中存取的所有用户信息
+ (NSMutableDictionary *)seg_getAllUserAndCommunityDictWithKey:(NSString *)key
{
    //取出所有用户和小区的数据
    NSMutableDictionary *allDict = [NSMutableDictionary dictionary];
    NSDictionary *tempDict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:key];
    if (tempDict) {
        [allDict addEntriesFromDictionary:tempDict];
    }
    return allDict;
}

//保存数据到key中
+ (void)seg_saveDataToUserDefaultWithKey:(NSString *)key data:(id)data
{
    NSMutableDictionary *allDict = [self seg_getAllUserAndCommunityDictWithKey:key];
    [allDict setObject:data forKey:[self seg_currentUserAndCommunity]];
    [[NSUserDefaults standardUserDefaults] setObject:allDict forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)seg_currentUserAndCommunity
{
    NSString *currentUserAndCommunity = [NSString stringWithFormat:@"%@,%@", kAccountUserID, kAccountCommunityID];
    return currentUserAndCommunity;
}

@end
